package com.jackrain.nea.common;

import com.alibaba.fastjson.JSONObject;
import com.jackrain.nea.config.PropertiesConf;
import com.jackrain.nea.core.redis.RedisObjectSerializer;
import com.jackrain.nea.web.query.QuerySessionImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;

import java.net.URI;
import java.util.HashMap;

/**
 * @author wuwenxue
 * @date 2018/05/18
 **/
@Service
public class PostDubbleHashMapEvent {

    @Autowired
    private PropertiesConf pconf;

    @Autowired
    private RestTemplateUtil restTemplateUtil;

    private RedisObjectSerializer serializer = new RedisObjectSerializer();

    public JSONObject executePostDubble(String notic, HashMap hashMap) throws Exception {

        String fcedas = pconf.getProperty("fc.edas.url", "");
        URI uri = new URI(fcedas + "/fc/api/execommand");

        JSONObject paramObject = new JSONObject();
        String[] trigger = notic.split(":");
        paramObject.put("address", trigger[0]);
        paramObject.put("group", trigger[2]);
        paramObject.put("version", trigger[1]);

        byte[] map = serializer.serialize(hashMap);

        paramObject.put("map", new String(map, "ISO-8859-1"));

        MultiValueMap<String, String> multiValueMap = new LinkedMultiValueMap<String, String>();

        multiValueMap.add("param", paramObject.toJSONString());
        String retStr = restTemplateUtil.doPostForm(uri, multiValueMap);
        JSONObject retJo = JSONObject.parseObject(retStr);

        return retJo;

    }

}
